package com.ease.framework.gef4p.figures.impl {
   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.commands.ChangePModelNameCommand;
   import com.ease.framework.gef4p.commands.EditAnnotationContentCommand;
   import com.ease.framework.gef4p.figures.ILabelFigure;
   import com.ease.framework.gef4p.model.Annotation;
   import com.ease.framework.gef4p.model.PModel;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.ICommand;

   import flash.events.FocusEvent;
   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.geom.Point;
   import flash.ui.Keyboard;

   import mx.controls.TextArea;

   public class AnnotationLabelFigureEditor extends TextArea {
      private var _labelFigure:ILabelFigure;

      //private var _contentLabelEditor:TextArea = new TextArea();

      public function AnnotationLabelFigureEditor() {
         super();

         addEventListener(FocusEvent.KEY_FOCUS_CHANGE, onFocusChange);
         addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, onFocusChange);
         addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

         addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
         addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
         addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
         addEventListener(MouseEvent.CLICK, onMouseClick);
      }

      public function setLabelFigure(location:Point, labelFigure:ILabelFigure):void {
         _labelFigure = labelFigure;


         if (labelFigure.editX != 0) {
            var point:Point = this.globalToLocal(new Point(labelFigure.editX, labelFigure.editY));
            this.x = point.x;
            this.y = point.y;
         }
         else {
            x = location.x;
            y = location.y;
         }

         this.width = _labelFigure.editWidth;
         this.height = _labelFigure.editHeight;

         this.text = _labelFigure.text;


      }

      public function beginEditText():void {
         _labelFigure.visible = false;
         this.visible = true;

         this.setSelection(0, 0xFFFFFF);
         this.setFocus();
      }

      protected function endEditText(change:Boolean):void {
         //this.visible = false;
         if (change) {
            //_labelFigure.text = this.text;
            //此处需要使用Command模式，来改变model对象的label.

            var pmodel:PModel = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByFigure(_labelFigure.getContainer()).model as PModel;
            var command:ICommand = null;
            if (pmodel is Annotation) {
               command = new EditAnnotationContentCommand(Annotation(pmodel), this.text);
            }
            else {
               command = new ChangePModelNameCommand(pmodel, this.text);
            }
            IEditor(Workbench.instance.currentEditor).getEditDomain().getCommandStack().push(command);

         }


         //this.removeChild( this );

         this.x = -1;
         this.y = -1;
         this.visible = false;
         //_labelFigure.visible = true;
         _labelFigure.setVisible(true);
         _labelFigure.refreash();
         //_labelFigure.selectionBeginIndex = -1;
         //_labelFigure.selectionEndIndex = -1;

         //UIComponent(_labelFigure).invalidateDisplayList();

      }

      protected function onFocusChange(event:FocusEvent):void {
         endEditText(true);
      }

      protected function onKeyDown(event:KeyboardEvent):void {
         event.stopImmediatePropagation(); // stop the main list from using alphabnumeric navigation

         if (event.keyCode == Keyboard.ENTER) {
            endEditText(true);
         }
//			super.measure();
      }

      protected function onMouseUp(event:MouseEvent):void {
         event.stopPropagation();
      }

      protected function onMouseDown(event:MouseEvent):void {
         event.stopPropagation();
      }

      protected function onMouseMove(event:MouseEvent):void {
         event.stopPropagation();
      }

      protected function onMouseClick(event:MouseEvent):void {
         event.stopPropagation();
      }

   }
}